"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库datetime类型
Case Name   : datetime类型插入日期0值验证，部分测试点合理报错
Create At   : 2023.08.09
Owner       : z@wan005
Description :
    1、创建数据库
    2、设置参数值，建表并插入0值日期验证；插入日期为全0值；日期不全为0值；日期范围内
    3、清理环境
Expect      :
    1、成功
    2、日期为全0值，成功；日期不全为0，报错
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class CompatibilityTest032(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.db_name = "db_datetime_0032"
        self.tb_name = "tb_datetime_0032"

    def test_atan(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:创建数据库 expect:成功-----'
        self.log.info(text)
        create_cmd = f"drop database if exists {self.db_name};" \
                     f"create database {self.db_name} dbcompatibility 'b';"
        msg = self.sh_primary.execut_db_sql(create_cmd)
        self.log.info(msg)
        self.assertIn('CREATE DATABASE', msg, '执行失败:' + text)

        text = '---step2:设置参数值，建表并插入0值日期验证 ' \
               'expect:日期为全0值，成功；日期不全为0，报错--'
        self.log.info(text)
        sql_cmd = f"set dolphin.sql_mode='sql_mode_strict," \
                  f"sql_mode_full_group,pipes_as_concat,ansi_quotes';" \
                  f"create  table {self.tb_name} (c1 datetime);" \
                  f"insert {self.tb_name} values('0000-00-00 00:00:00');" \
                  f"insert {self.tb_name} values('0000-10-00 14:20:00');" \
                  f"insert {self.tb_name} values('0000-00-01 14:20:00');" \
                  f"insert {self.tb_name} values('4714-11-24 00:00:00bc');" \
                  f"insert {self.tb_name} " \
                  f"values('294277-1-9 4:00:54.775807');" \
                  f"select * from {self.tb_name};"
        msg = self.sh_primary.execut_db_sql(sql_cmd,
                                            dbname=self.db_name)
        self.log.info(msg)
        self.assertIn('SET', msg, '执行失败:' + text)
        self.assertIn('CREATE TABLE', msg, '执行失败:' + text)
        self.assertEqual(3, msg.count('INSERT 0 1'), '执行失败:' + text)
        res_list = ['0000-00-00 00:00:00', '4714-11-24 00:00:00', 'infinity']
        for res in res_list:
            self.assertIn(res, msg, '执行失败:' + text)
        self.assertEqual(2, msg.count(
            'ERROR:  date/time field value out of range'), '执行失败:' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.tb_name};"
        msg = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(msg)
        self.assertIn('DROP DATABASE', msg, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
